package restless.menu {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import restless.core.gameObjs.RLETextObj;
	import restless.core.gameObjs.RLEBlitMC;
	import restless.core.gameObjs.RLEManagedObj;
	import restless.utilities.numeric.RLEMath;
	
	/**
	 * 
	 * @author Jordan Laine
	 * 
	 */
	
	public class LoadingScreen extends RLEManagedObj {
		
		private var percent:Number;
		private var loadingBKG:RLEBlitMC;
		private var sLoading:String = "Loading";
		private var sPercent:String = "Percent " + String( RLEMath.roundTo(percent, -2) );
		private var textObj1:RLETextObj;
		private var textObj2:RLETextObj;
		
		public function LoadingScreen() {
			setRE();
			rect = screenM.rect.clone();
			updating = true;
			gfxM.parseTimerInterval = 60;
			gfxM.startTimerParse();
			setText();
		}
		
		public function setBkg(bkg:*):void {
			trace (bkg);
			loadingBKG = new RLEBlitMC();
			loadingBKG.setRE();
			loadingBKG.sID = "LoadingScreen BKG";
			if (bkg is MovieClip) {
				loadingBKG.switchGFXMC(bkg as MovieClip);
			} else if (bkg is Bitmap) {
				loadingBKG.setSingleImage(bkg.bitmapData);
			} else if ( bkg is BitmapData) {
				loadingBKG.setSingleImage(bkg);
			}
			
			loadingBKG.updating = true;
			loadingBKG.backgroundObj = true;
		}
		
		public function setText():void {
			textM.addTextFormat("loading", textM.DEFAULT_FONT, 0x999999);
			textObj1 = addText("textObj1", sLoading, "loading", 0, (rect.height * .5) );
			textObj1.x = rect.width * .5 + textObj1.centerX;
			textObj2 = addText("textObj2", sPercent, "loading", 0, (rect.height * .5 + 32) );
			textObj2.x = rect.width * .5 + textObj2.centerX;
			textObj1.overlaying = true;
			textObj2.overlaying = true;
		}
		
		override public function update():void {
			percent = gfxM.getPercentParsed();
			sPercent = String(Math.round(percent * 100)) + " %";
			textObj2.text = sPercent;
			//trace(percent + " LoadingScreen!!");
			
			textObj1.x = rect.width * .5 + textObj1.centerX;
			textObj2.x = rect.width * .5 + textObj2.centerX;
			
			if (percent >= 1) { 
				doneLoading(); 
			}
			
		}
		
		public function doneLoading():void {
			RE.switchSystemState(RE.nextSystemState);
		}
		
		override public function dispose():void {
			removeAllText();
			textObj1 = null;
			textObj2 = null;
			loadingBKG.dispose();
			loadingBKG = null;
			super.dispose();
		}
	}
}